package lancooUpLoad;

import android.content.DialogInterface;

import com.example.lancooznbk.presenter.PaperDef;
import com.example.lancooznbk.presenter.presenter_interface.IBaseReviewPresenter;
import com.example.lancooznbk.view.base.ILoadPaperView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;

import testPaperCTCS.GuessTestingPaperActivity;
import testPaperJBFX.BasicReviewPaperActivity;
import testPaperNDFX.DifficultReviewPaperActivity;

//import static lancooUpLoad.PaperInfor.isfirstSubmit;
import static testPaperCTCS.GuessTestingPaperActivity.InforGuessTesting;
import static testPaperJBFX.BasicReviewPaperActivity.InforBasicReview;
import static testPaperNDFX.DifficultReviewPaperActivity.InforDifficultReview;

/**
 * Created by Administrator on 2017/8/24/024.
 */

public class SubmitPresenter implements IBaseReviewPresenter {

    private ILoadPaperView view;
    private int PaperFrom;
    private boolean isFromExit = false;//

    public SubmitPresenter(ILoadPaperView view, int paperFrom) {
        this.view = (ILoadPaperView) view;
        this.PaperFrom = paperFrom;
    }

    @Override
    public void getData(boolean isShowLoading) {
          //if (isShowLoading) view.showLoading();
        HttpOnNextListener<SubmitAnswerEntityResult> httpOnNextListener = new HttpOnNextListener<SubmitAnswerEntityResult>() {

            @Override
            public void onNext(SubmitAnswerEntityResult submitAnswerEntityResult) {

                if(submitAnswerEntityResult.getValue().equals("1")){

                    if (isFromExit){
                        //ToastUtils.showShort("数据提交成功");
                        if(PaperFrom == PaperDef.FROM_BASE_REVIEW) {
                            InforBasicReview.setState(2);
                        }else if(PaperFrom == PaperDef.FROM_BASE_REVIEW_WRONG){

                        }else if(PaperFrom == PaperDef.FROM_DIFFERENT_REVIEW){
                            InforDifficultReview.setState(2);
                        }else if(PaperFrom == PaperDef.FROM_DIFFERENT_REVIEW_WRONG){

                        }else if(PaperFrom == PaperDef.FROM_GUESS_REVIEW){
                            InforGuessTesting.setState(2);
                        }

//                        isfirstSubmit = true;

                        view.getLoadingDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                                view.openExamReportDialog();
                            }
                        });
                        view.updatingResult(true);

                       /* new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {

                            }
                        },100);*/

                    }

                }else{
                    view.updatingResult(false);
                    ToastUtils.showShort("数据保存失败! 错误码"+submitAnswerEntityResult.getValue());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.updatingResult(false);
            }
        };

        SubmitParam api = null;
        if(PaperFrom == PaperDef.FROM_BASE_REVIEW) {
            api = new SubmitParam(httpOnNextListener, (BasicReviewPaperActivity) view);
        }else if(PaperFrom == PaperDef.FROM_BASE_REVIEW_WRONG){

        }else if(PaperFrom == PaperDef.FROM_DIFFERENT_REVIEW){
            api = new SubmitParam(httpOnNextListener, (DifficultReviewPaperActivity) view);
        }else if(PaperFrom == PaperDef.FROM_DIFFERENT_REVIEW_WRONG){

        }else if(PaperFrom == PaperDef.FROM_GUESS_REVIEW){
            api = new SubmitParam(httpOnNextListener, (GuessTestingPaperActivity) view);
        }


        api.setListSubmitDataJson(PaperInfor.ListSubmitDataJson);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getMoreData() {
        getData(false);
    }

    @Override
    public void getDataByStatus(int position) {
        getData(false);
    }

    public void getData(boolean isShowing ,boolean isFromExit){
        this.isFromExit = isFromExit;
        getData(isShowing);
    }

}